package com.lanchetech.job;


import com.lanchetech.service.QuartzService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class WaitPayJob extends QuartzJobBean {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private Scheduler scheduler;

    private  QuartzService quartzService;

    @Autowired
    public void WaitPayJob(Scheduler scheduler, QuartzService quartzService) {
        this.scheduler = scheduler;
        this.quartzService = quartzService;
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext){
        Trigger trigger = jobExecutionContext.getTrigger();
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        // 将添加任务的时候存进去的数据拿出来
        Long id = jobDataMap.getLong("orderId");

        quartzService.closeOrder(id);
        log.info("关闭未支付的订单，编号为{}",id);

        // 执行之后删除任务
        try {
            // 暂停触发器的计时
            scheduler.pauseTrigger(trigger.getKey());
            // 移除触发器中的任务
            scheduler.unscheduleJob(trigger.getKey());
            // 删除任务
            scheduler.deleteJob(jobDetail.getKey());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
